package net.jalbum.filters;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;

import net.jalbum.filters.components.ExtendedSlider;
import se.datadosen.component.RiverLayout;
import se.datadosen.jalbum.Msg;


public class ColorAdjustmentControl extends FilterControl {
	/**
	 *
	 */
	private static final long serialVersionUID = 2472355637938881568L;
	protected ColorAdjustmentFilterPlugin colorAdjustmentFilterPlugin;
	protected ExtendedSlider sliderBrightness;
        protected ExtendedSlider sliderContrast;
	protected ExtendedSlider sliderSaturation;

	protected JCheckBox absoluteBrightness;
	protected JButton resetAll;

	public ColorAdjustmentControl(){}

	public ColorAdjustmentControl(ColorAdjustmentFilterPlugin colorAdjustmentFilterPlugin) {
		init(colorAdjustmentFilterPlugin);

		addColorAdjustmentControls();
	}

	protected void addColorAdjustmentControls() {
		sliderBrightness = new ExtendedSlider(-100,100,0);
		sliderBrightness.getSlider().setPaintLabels(false);
		sliderBrightness.setSliderWidth(SLIDER_WIDTH);
		sliderBrightness.removeResetButton();
		sliderBrightness.addActionListener(this);
		absoluteBrightness = new JCheckBox();
		absoluteBrightness.addActionListener(this);

                sliderContrast= new ExtendedSlider(-100,100,0);
		sliderContrast.getSlider().setPaintLabels(false);
		sliderContrast.setSliderWidth(SLIDER_WIDTH);
		sliderContrast.removeResetButton();
		sliderContrast.addActionListener(this);
		
		sliderSaturation = new ExtendedSlider(-100,100,0);
		sliderSaturation.getSlider().setPaintLabels(false);
		sliderSaturation.setSliderWidth(SLIDER_WIDTH);
		sliderSaturation.removeResetButton();
		sliderSaturation.addActionListener(this);
						
		resetAll= new JButton(Msg.getString(this,"filters.colorAdjustment.resetAll"));
		resetAll.addActionListener(this);

                this.add(new JLabel(Msg.getString(this,"filters.colorAdjustment.brightness")));
		this.add("br", sliderBrightness);
		//		this.add("br", absoluteBrightness);
		//		this.add(new JLabel(Msg.getString(this,"filters.colorAdjustment.absolteBrightness")));

		this.add("p", new JLabel(Msg.getString(this,"filters.colorAdjustment.contrast")));
		this.add("br", sliderContrast);

		this.add("p", new JLabel(Msg.getString(this,"filters.colorAdjustment.saturation")));
		this.add("br", sliderSaturation);
		this.add("br",resetAll);
	}

	protected void init(ColorAdjustmentFilterPlugin colorAdjustmentFilterPlugin) {
		this.colorAdjustmentFilterPlugin = colorAdjustmentFilterPlugin;
		super.init(colorAdjustmentFilterPlugin);
                setLayout(new RiverLayout(6,2));
	}

	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		if(e.getSource()==sliderContrast){
			if(sliderContrast.getValue()!=colorAdjustmentFilterPlugin.getContrast()){
				colorAdjustmentFilterPlugin.adjustContrast(sliderContrast.getValue());
			}
		}else if(e.getSource()==sliderSaturation){
			if(sliderSaturation.getValue()!=colorAdjustmentFilterPlugin.getSaturation()){
				colorAdjustmentFilterPlugin.adjustSaturation(sliderSaturation.getValue());
			}
		}else if(e.getSource()==sliderBrightness){
			if(sliderBrightness.getValue()!=colorAdjustmentFilterPlugin.getBrightness()){
				colorAdjustmentFilterPlugin.adjustBrightness(sliderBrightness.getValue());
			}
		}else if(e.getSource()==absoluteBrightness){
			colorAdjustmentFilterPlugin.setAbsoluteBrightness(absoluteBrightness.isSelected());
			colorAdjustmentFilterPlugin.adjustBrightness(sliderBrightness.getValue());
		}else if(e.getSource()==resetAll){
			sliderContrast.reset();
			sliderSaturation.reset();
			sliderBrightness.reset();
		}
	}
}
